package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.constant.MessageConstant;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.PeopleDTO;
import com.sky.dto.PeopleLoginDTO;
import com.sky.entity.Employee;
import com.sky.entity.People;
import com.sky.exception.AddressBookBusinessException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.mapper.PeopleMapper;
import com.sky.service.EmployeeService;
import com.sky.service.PeopleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 登录
     * @param employeeLoginDTO
     * @return
     */
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String employeeName=employeeLoginDTO.getEmployeeName();
        String password=employeeLoginDTO.getPassword();
        //根据用户名查询数据库中的数据
        LambdaQueryWrapper<Employee> lam=new LambdaQueryWrapper<>();
        lam.eq(Employee::getEmployeeName,employeeName);
        Employee employee=employeeMapper.selectOne(lam);
        //处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if(employee==null){
            //账号不存在
            throw new AddressBookBusinessException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码对比
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(employee.getPassword())){
            //密码不对
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        //返回实体对象
        return employee;
    }



}
